﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using UPMS.Core.Domain;

namespace UPMS.Web.Controllers
{
    public class ApplicationController : BaseController
    {
        // GET: /Application/
        public ActionResult Index()
        {

            return View(appDao.Select());
        }

        // GET: /Application/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Application application = appDao.Select(id.Value);
            if (application == null)
            {
                return HttpNotFound();
            }
            return View(application);
        }

        // GET: /Application/Create
        public ActionResult Create()
        {
            ViewBag.HostId = new SelectList(hostDao.Select(), "Id", "Name");
            return View();
        }

        // POST: /Application/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,HostId,Description,Created,CreatedBy,Modified,ModifiedBy")] Application application)
        {
            if (ModelState.IsValid)
            {
                application.Id = Guid.NewGuid();
                appDao.Insert(application);
                return RedirectToAction("Index");
            }

            ViewBag.HostId = new SelectList(hostDao.Select(), "Id", "Name", application.HostId);
            return View(application);
        }

        // GET: /Application/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Application application = appDao.Select(id.Value);
            if (application == null)
            {
                return HttpNotFound();
            }
            ViewBag.HostId = new SelectList(hostDao.Select(), "Id", "Name", application.HostId);
            return View(application);
        }

        // POST: /Application/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,HostId,Description,Created,CreatedBy,Modified,ModifiedBy")] Application application)
        {
            if (ModelState.IsValid)
            {
                appDao.Update(application);
                return RedirectToAction("Index");
            }
            ViewBag.HostId = new SelectList(hostDao.Select(), "Id", "Name", application.HostId);
            return View(application);
        }

        // GET: /Application/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Application application = appDao.Select(id.Value);
            if (application == null)
            {
                return HttpNotFound();
            }
            return View(application);
        }

        // POST: /Application/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            appDao.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
